<html>
<head><meta charset="utf-8"><title>Range complaints · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html">Range complaints</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211596812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211596812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Duchniewicz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211596812">(Sep 29 2020 at 09:09)</a>:</h4>
<p>I recently stumbled upon ridiculous fish's blogpost describing ranges and how they are 'broken' in some cases.<br>
<a href="https://ridiculousfish.com/blog/posts/least-favorite-rust-type.html">https://ridiculousfish.com/blog/posts/least-favorite-rust-type.html</a></p>
<p>What are your opinions on that? I am especially curious about no-check for end &lt;= start which results in panics when indexing via it.</p>



<a name="211602645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211602645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Goat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211602645">(Sep 29 2020 at 10:17)</a>:</h4>
<p>Ranges are definitely far below their intended ergonomics right now, I fully agree with the points that the post makes.</p>
<p>The question here is not as much about how we could improve ranges as it is about how we could fit in a "better range type" while still being compatible and interoperable with code using the 2015 and 2018 editions, since the standard library can't just replace the range types with their new and improved counterparts. That would either cause mass breakage in code or be not substantial enough of an improvement to be enough to bring ranges to desirable quality standards.</p>
<p>My personal preferred solution is to introduce "conditional reexports" into the language, i.e. reexports which are only visible if the code observing them was built with a specified <em>range</em> of editions.</p>
<p>Let's say that there's a <code>compat</code> module inside of <code>std</code>/<code>core</code>. Inside that module will reside the <code>range</code> module and its submodule <code>v1</code>, which would be home to the current range types as they are, alone with a <code>v2</code> submodule, which would contain the new range types. Then, <code>core::ops</code> would have a conditional reexport of <code>core::compat::range::v1::*</code> if under edition 2021 (or whichever edition will introduce the change), and <code>core::compat::range::v2::*</code> under 2021+. The contents of <code>v1</code> would implement <code>From</code> for their <code>v2</code> counterparts and vice versa, so that 2021 edition code could convert its ranges before passing them to 2018 edition code.</p>
<p>This model would allow us to do all sorts of breaking changes to the range types, such as removing <code>Iterator</code> (by delegating it to a dedicated <code>RangeIter</code>, which contains a <code>Range</code> and iterates over it), adding <code>Copy</code>, adding a struct-level bound for <code>PartialOrd</code> and <code>Step</code>, making the fields private to avoid <code>end &lt;= start</code> ranges, and everything else we'd want to consider ranges a convenient universal type and not a niche indexing syntax toy.</p>
<p>That'd also require adding lang item hooks to ensure that range syntax in edition 2021+ uses <code>v2</code>, and the same syntax in 2015/2018 uses <code>v1</code>. Not the hardest part in all this, as compared to the base "conditional reexports" thing. By the way, we could also make it usable in user crates by specifying containing crate semver ranges instead of edition ranges, so that the Broader Ecosystem could benefit from nonbreaking seamless compatibility too.</p>



<a name="211602904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211602904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211602904">(Sep 29 2020 at 10:20)</a>:</h4>
<p>People are already working on a solution though <a href="https://github.com/rust-lang/rust/pull/77180">https://github.com/rust-lang/rust/pull/77180</a></p>



<a name="211606969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211606969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Goat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211606969">(Sep 29 2020 at 11:14)</a>:</h4>
<p>The <code>MustClone</code> solution seems very hacky and unidiomatic to me, and it looks like a temporary measure rather than a full-blown long-term solution, since it doesn't solve other problems with ranges. I'm sure that Conditional Reexports would be useful if we find something hacky and annoying in the standard library again, if ever; not just for the specific case with ranges.</p>



<a name="211607777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211607777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211607777">(Sep 29 2020 at 11:24)</a>:</h4>
<p>There's nothing unidiomatic about something users cannot write on stable. Hacky? Sure, but we have done way worse.</p>



<a name="211609740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211609740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Goat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211609740">(Sep 29 2020 at 11:48)</a>:</h4>
<blockquote>
<p>Hacky? Sure, but we have done way worse.</p>
</blockquote>
<p>I don't think that past experience with ugly hacks is a valid justification for continuing to implement similar hacks to problems which can be solved properly and more substantially, even if that would take more time. Mistakes do happen in programming language design, but when they do, no one should accept them as "prior art" — those ought to be accepted as pieces of history and as a lesson of what <em>not</em> to do.</p>



<a name="211610276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211610276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211610276">(Sep 29 2020 at 11:54)</a>:</h4>
<p>If <a href="https://github.com/rust-lang/rust/issues/77180">#77180</a> merged, does it prevent us from removing it in the future without breaking code ?</p>



<a name="211610948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211610948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Goat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211610948">(Sep 29 2020 at 12:01)</a>:</h4>
<p>Well, <code>MustClone</code> would become a standard library trait, which would mean that external code could implement it or even have trait bounds on it (the latter wouldn't make sense but is possible anyway). After that, <code>MustClone</code> could only become deprecated but not removed, so we wouldn't really be able to undo the hack once we implement a proper solution.</p>



<a name="211611288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211611288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211611288">(Sep 29 2020 at 12:04)</a>:</h4>
<p>MustClone would probably not be stabilized, at least in the initial draft, so we could definitely remove it in the future.</p>



<a name="211611952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211611952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Goat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211611952">(Sep 29 2020 at 12:11)</a>:</h4>
<p>So it will be a trait for signalling the compiler that a type is <code>Copy</code> but still has manual copying semantics, which will be <em>unstable to implement or refer to</em> by itself but the manual copy semantics that it provides would indeed be visible to stable code, as well as the <code>Copy</code> implementations. Am I understanding this right?</p>
<p>There's an issue I'm seeing with this though: if it gets removed without stabilization, types from <code>std</code> which implemented <code>MustClone</code> and thus <code>Copy</code> together with <code>Iterator</code> will no longer be able to implement <code>Copy</code>, and that would be a breaking change to all code which depended on their <code>Copy</code> implementation. So we can only implement this on ranges if we're planning to rework them anyway, or on iterators as well but then we'd need a way to enforce the same semantics without the hack.</p>



<a name="211612294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211612294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211612294">(Sep 29 2020 at 12:15)</a>:</h4>
<p>We'd certainly need <em>some</em> solution that's an alternative to it if we were to remove it, but there's nothing that would tie us to that particular approach.</p>



<a name="211613130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211613130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211613130">(Sep 29 2020 at 12:24)</a>:</h4>
<p>I mean we could also just make all ranges language items and special case them that way, a trait just expresses this better. (The fact that ranges are <em>not</em> language items seems weird anyways, we desugar range syntax to it)</p>



<a name="211780891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Range%20complaints/near/211780891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Range.20complaints.html#211780891">(Sep 30 2020 at 15:50)</a>:</h4>
<p>There's also a conversation about this in the libs stream: <a href="#narrow/stream/219381-t-libs/topic/ridiculousfish.20Range.20feedback/near/211031625">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/ridiculousfish.20Range.20feedback/near/211031625</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>